<template>
  <div class="content">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="query"
        class="demo-form-inline"
      >
        <el-form-item label="部门">
          <el-select v-model="query.department_id">
            <el-option label="所有部门" :value="0"></el-option>
            <el-option
              v-for="item in departmentOption"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位">
          <el-select v-model="query.station_id">
            <el-option label="所有岗位" :value="0"></el-option>
            <el-option
              v-for="item in stationOption"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名|电话">
          <el-input
            v-model="query.username"
            placeholder="请输入面试人姓名或联系电话"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getTableInfo"
            >查询</el-button
          >
          <el-button
            type="primary"
            icon="Plus"
            @click="handleInfo"
            v-auth="'interview_add'"
            >添加面试</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" row-key="id" height="100%" stripe>
      <el-table-column type="index" label="#" fixed="left" />
      <el-table-column prop="name" label="姓名" fixed="left" />
      <el-table-column prop="mobile" label="联系方式" fixed="left" />
      <el-table-column prop="department.title" label="所属部门" />
      <el-table-column prop="station.title" label="面试岗位" />
      <el-table-column label="简历">
        <template #default="scope">
          <el-link :href="scope.row.file" type="primary">查看简历</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="order_time" sortable label="面试时间" />
      <el-table-column prop="status" sortable label="当前状态" />
      <el-table-column prop="result_text" sortable label="面试结果" />
      <el-table-column label="操作" fixed="right" width="300">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleLog(scope.row.id)"
          >
            面试记录
          </el-button>
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleInfo(scope.row.id)"
            v-auth="'interview_edit'"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            link
            icon="DeleteFilled"
            @click="delInfo(scope.row.id)"
            v-auth="'interview_del'"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider border-style="none" />
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <Drawer
      ref="infoDrawer"
      @getTable="getTableInfo"
      :department="departmentOption"
    />
    <LogDrawer ref="logDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import { getDepartmentOptionsApi } from "@/api/company/department";
import Drawer from "./component/dialogForm.vue";
import LogDrawer from "./component/dialogLog.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { delApi, getListAPi } from "@/api/company/interview";
import { getListAPi as getStationApi } from "@/api/company/station";
const infoDrawer = ref<any>(null);
const logDrawerRef = ref<any>(null);
const departmentOption = ref<any[]>([]);
const stationOption = ref<any[]>([]);
const query = reactive({
  department_id: 0,
  station_id: 0,
  username: "",
});
const total = ref(0);
watch(
  query,
  () => {
    getTableInfo();
  },
  { deep: true }
);
watch(
  () => query.department_id,
  (newVal) => {
    if (newVal) {
      getStationOptions();
    } else {
      stationOption.value = [];
    }
  }
);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
onMounted(() => {
  getDepartmentOptionsApi().then((res) => {
    res.data.forEach((el) => {
      departmentOption.value.push({
        id: el.id,
        title: el.title,
      });
      if (el.children && el.children.length) {
        el.children.forEach((el) => {
          departmentOption.value.push({
            id: el.id,
            title: el.title,
          });
        });
      }
    });
  });
  getTableInfo();
});
const getStationOptions = () => {
  getStationApi({
    department_id: query.department_id,
  }).then((res) => {
    stationOption.value = res.data;
  });
};
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};

function getTableInfo() {
  getListAPi({
    page: curPage.value,
    page_size: pageSize.value,
    ...query,
  }).then((res) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除此条面试信息吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi(id).then((res: any) => {
        getTableInfo();

        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

const handleInfo = (id?: number) => {
  infoDrawer.value.dialogOpen(id);
};
const handleLog = (id?: number) => {
  logDrawerRef.value.dialogOpen(id);
};
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
